<?php
/**
 * User: yunfengvico@163.com
 * Date: 2018/11/6 15:10
 * Desc:
 */
namespace App\Models;

class ScoreGoods extends BaseModel
{
    protected $rules = [
        'goods_name' => 'required',
        'goods_score' => 'required|integer|min:1',
    ];
    protected $message = [
        'goods_name.required' => '请输入商品名称',
        'goods_score.required' => '请输入商品积分价格',
        'goods_score.integer' => '商品积分价格应为整数',
        'goods_score.min' => '商品积分价格最小应为1',
    ];
    protected $table = 'score_goods';
    protected $redisListTable = "score_goods_list";

    public function getScoreGoodsList($where)
    {
        $keyword = request('keyword');
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['id'] = $keyword;
            } else {
                $where['goods_name'] = ['like', '%' . $keyword . '%'];
            }
        }
        $rows = $this->_getPageList($where);
        $txtStr['status'] = [1 => '上架', -1 => '下架', 0 => '待上架'];
        $rows['data'] = int_to_string($rows['data'], $txtStr);
        return $rows;
    }

    public function addOrUpdate()
    {
        $shopId = request('shop_id',0);
        if(!$shopId) {
            $this->error = '非法操作';
            return false;
        }

        $data['admin_id'] = plat_admin_id();
        $data['shop_id'] = $shopId;
        $data['goods_name'] = trim(request('goods_name'));
        $data['goods_score'] = request('goods_score', 1);
        $data['pic_num'] = count(request('pics'));
        $data['category_id'] = request('category_id');
        $data['bucket_category_id'] = request('bucket_category_id');
        $data['goods_content'] = trim(request('goods_content',''));
        if (request('id')) {
            $data['id'] = trim(request('id'));
        }
        if (!$this->_validate($data)) {
            return false;
        }
        $this->_start();
        try{
            $res = $this->_save($data);
            if (!$res) {
                $this->error = '保存商品信息失败！';
                $this->_rollBack(); //回滚
                return false;
            }
            $id = isset($data['id']) ? $data['id'] : $res;
            $picture = new Picture();
            $savePath = '/uploads/goods/score/';
            $res = $picture->upload($id, $savePath);
            if (!$res) {
                $this->_rollBack(); //回滚
                $this->error = $picture->getError();
                return false;
            }

            $this->_commit();
            return true;
        }catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->_rollBack();
        }
    }

    public function _redisDelScoreGoodsListByKey($key)
    {
        if (!$key) {
            $this->error = '非法操作';
            return false;
        }
        $table = $this->redisListTable;
        $this->redis->hdel($table, $key);
    }

    public function _redisGetScoreGoodsListKeysBySid($sid)
    {
        if (!$sid) {
            $this->error = '非法操作';
            return false;
        }
        $table = $this->redisKeyTable;
        $key = $sid;
        $res = $this->redis->hmget($table, $key);
        if ($res) {
            return unserialize($res[0]);
        }
        return false;
    }

    public function _redisGenerateScoreGoodsListBySid($sid)
    {
        if (!$sid) {
            $this->error = '非法操作';
            return false;
        }
        $keys = $this->_redisGetScoreGoodsListKeysBySid($sid);
        if ($keys && is_array($keys)) {
            foreach ($keys as $v) {
                $this->_redisDelScoreGoodsListByKey($v);
            }
        }
    }

    public function _redisGenerateScoreGoodsListByAid($aid)
    {
        $shop = new Shop();
        $res = $shop->_getList(['admin_id' => $aid], 'id')->toArray();
        if ($res) {
            foreach ($res as $v) {
                $keys = $this->_redisGetScoreGoodsListKeysBySid($v['id']);
                if ($keys && is_array($keys)) {
                    foreach ($keys as $v) {
                        $this->_redisDelScoreGoodsListByKey($v);
                    }
                }
            }
        }

    }
}